<template>
  <div class="login-div">
    <div class="login-box">
      <div class="login-content">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 350px">
          <div class="login-title">喵汪云管家</div>
          <el-form-item prop="username">
            <el-input size="large" v-model="data.form.id" placeholder="请输入账号" prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input size="large" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock" show-password></el-input>
          </el-form-item>
        </el-form>
        <div class="login-buttons">
          <el-button type="primary" @click="login" size="large" style="width: 50%">登录</el-button>
          <el-button type="warning" @click="clear" size="large" style="width: 50%">清除</el-button>
        </div>
        <div class="login-footer">
          还没有账号？请<a class="register-link" href="/register"> 注册</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref,reactive} from "vue";
import {User, Lock, Position} from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const data = reactive({
  form:{},
  rules: {
    id:[
      {required:true,message:'请输入账号',trigger:'blur'}
    ],
    password:[
      {required:true,message:'请输入密码',trigger:'blur'}
    ]
  }
})

const formRef= ref()
const clear = ()=>{
  data.form.username="";
  data.form.password="";
}

const login = ()=>{
  //发送登录请求
  formRef.value.validate((valid)=>{
    if(valid){
      request.post('/employee-pet/employee-pet/login',data.form).then(res=>{
        console.log(res)
        if(res.code=='200'){
          //存储后台返回的信息
          localStorage.setItem('xm-pro-user',JSON.stringify(res.data));  //把json对象转化为字符串
          ElMessage.success("登录成功");
          location.href='/manager/home';
        }else{
          ElMessage.error(res.msg)
        }
      }).catch(err => {
        console.error('完整错误:', err);
        console.error('响应数据:', err.response?.data);
      });
    }
  })
}
</script>

<style scoped>
.login-div {
  height: 100vh;
  display: flex;
  justify-content: flex-end; /* 将内容推到右侧 */
  align-items: center; /* 垂直居中 */
  background-image: url("@/assets/login.png");
  background-size: cover;
  background-position: 0 40px;
  padding-right: 10%; /* 右侧留白 */
}

.login-box {
  width: 400px; /* 固定宽度 */
  display: flex;
  justify-content: center;
}

.login-content {
  background-color: #ffdbef;
  padding: 50px 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgb(0,0,0,0.2);
  width: 100%;
}

.login-title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
}

.login-buttons {
  display: flex;
  gap: 10px;
}

.login-footer {
  margin-top: 10px;
  text-align: right;
}

.register-link {
  color: #73aee6;
  text-decoration: none;
}

.register-link:hover {
  text-decoration: underline;
}

/* 响应式设计 - 在小屏幕上调整布局 */
@media (max-width: 768px) {
  .login-div {
    justify-content: center;
    padding-right: 0;
  }

  .login-box {
    width: 90%;
    max-width: 400px;
  }

  .login-content {
    padding: 30px 15px;
  }
}
</style>
